<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GradeComputation
 *
 * @author Emerson
 */
class GradeComputation {
    //put your code here
    
    public function computeSubjectAverage($idstud, $idsub, $quarter,$level,$archived) {
       
        //Select grade criterion of subject
        $gradeCriterionList = mysql_query("SELECT * FROM gradecriterion WHERE Subject_idSubject=".$idsub." AND quarter='".$quarter."'");
        
        $rAverage=0;
        while($row=mysql_fetch_array($gradeCriterionList))
        {
            extract($row);
            
            //select grades based on grade criteria selected
            $gradeList = mysql_query("SELECT * FROM grade WHERE gradecriterion_idGradeCriteria='$idGradeCriteria' AND student_idStudent='$idstud' AND archived='".$archived."' AND level='$level'");
            $average=0;
            $counter=0;
            while($row1=mysql_fetch_array($gradeList))
            {
                extract($row1);
                //add grades 
                $average+=$grade;
                $counter++;
            }
            
            //divide accumulation of grades by counter
            if($counter!=0)
                $average/=$counter;
            
           
            $percentage/=100;
            
            //accumulation multiplied to percentage
            $average*=$percentage;
            
            //add to total
            $rAverage+=$average;
        }
        return round($rAverage);
        
    }
    
    public function computeQuarterAverage($idstud, $quarter,$level,$archived) {
        
        //get all subjects of student
        $studentSubjects = mysql_query("SELECT * FROM student_has_subject WHERE idStudent='$idstud'");
        $average=0;
        $counter=0;
        
        while($row=mysql_fetch_array($studentSubjects))
        {
            extract($row);
            
            //compute average of subject in a qurter and add to total
            $average+=$this->computeSubjectAverage($idstud, $idSubject, $quarter,$level,$archived);
            $counter++;
        }
        //divide total by counter to get average
        $average/=$counter;
        return intval($average);
    }
    
    public function computeSubjectTotalAverage($first, $second, $third, $fourth) {
        
        //add quarter averages of a subject
        
        $average=$first+$second+$third+$fourth;
        
        //divide by 4
        return round($average/4,2);
    }
    
    public function computeTotalAverage($idStudent,$level,$archived) {
        
        //select all subjects of student
        $studentSubjects = mysql_query("SELECT * FROM student_has_subject WHERE idStudent='$idStudent'");
        $average=0;
        $counter=0;
        while($row=mysql_fetch_array($studentSubjects))
        {
            extract($row);
            
            //get total average of subject and add to total average
            $first=$this->computeSubjectAverage($idStudent, $idSubject,"1st Quarter",$level,$archived);
            $second=$this->computeSubjectAverage($idStudent, $idSubject,"2nd Quarter",$level,$archived);
            $third=$this->computeSubjectAverage($idStudent, $idSubject,"3rd Quarter",$level,$archived);
            $fourth=$this->computeSubjectAverage($idStudent, $idSubject,"4th Quarter",$level,$archived);
            $average+=$this->computeSubjectTotalAverage($first,$second,$third,$fourth);
            $counter++;
        }
        
        //total average = total average divided by counter
        $average/=$counter;

        return round($average,2);
    }
}



?>



